package com.mall.shopping.services;

import com.mall.shopping.ICartUpdateService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.CartProductDto;
import com.mall.shopping.dto.UpdateCartNumRequest;
import com.mall.shopping.dto.UpdateCartNumResponse;
import org.apache.dubbo.config.annotation.Service;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @author pengyehua
 * @date 2020/12/21 16:47
 */
@Service
@Component
public class CartUpdateServiceImpl implements ICartUpdateService {


    @Autowired
    RedissonClient redissonClient;

    @Override
    public UpdateCartNumResponse updateCart(UpdateCartNumRequest request) {
        UpdateCartNumResponse response = new UpdateCartNumResponse();

        //判空
        request.requestCheck();
        //从redis取出购物车数据
        String userId = request.getUserId().toString();
        String itemId = request.getItemId().toString();
        RMap<Object, Object> map = redissonClient.getMap(userId);
        CartProductDto cartProductDto = (CartProductDto) map.get(itemId);
        //判断购物车指定商品是否为空
        if(cartProductDto == null){
            response.setCode(ShoppingRetCode.ITEM_HAD_DELETED.getCode());
            response.setMsg(ShoppingRetCode.ITEM_HAD_DELETED.getMessage());
            return response;
        }


        //修改checked和num状态
        cartProductDto.setChecked(request.getChecked());
        cartProductDto.setProductNum((long)request.getNum());
        map.put(itemId,cartProductDto);
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return response;
    }
}
